/// <reference path="../lib/lib.ts" />

class Controller {

	static pressed : any;

	constructor() {
		Controller.pressed = [];
		document.onkeyup = this.keyUp;
		document.onkeydown = this.keyDown;
	}
	
	keyDown(e) {
	var ia = $.inArray(e.keyCode, Controller.pressed);
		if($.inArray(e.keyCode, Controller.pressed) != -1)
			return false;
			
		var key = e.keyCode;
		Controller.pressed.push(key);
		switch(key) {
			// w
			case 87:
				Game.getInstance().unitMove(Unit.FORWARD);
				break;
			// d
			case 68:
				Game.getInstance().unitMove(Unit.RIGHTWARD);
				break;
			// s
			case 83:
				Game.getInstance().unitMove(Unit.BACKWARD);
				break;
			// a
			case 65:
				Game.getInstance().unitMove(Unit.LEFTWARD);
				break;
		}
	}
	
	keyUp (e) {
		delete Controller.pressed[Controller.pressed.indexOf(e.keyCode)];
		Game.getInstance().unitStop();
	} 
}